package com.example.eventdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imgView = null;
    private Button btn = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgView = (ImageView) findViewById(R.id.img);
        btn = (Button) findViewById(R.id.btn);
        // 匿名内部类作监听器
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                imgView.setImageResource(R.drawable.lijiang);
//            }
//        });
        // 匿名内部类作监听器
//        btn.setOnClickListener(new MyListener());
        // Activity本身当作监听器
        //btn.setOnClickListener(this);
    }

    // activity本身当作监听器
    public void onClick(View v) {
        imgView.setImageResource(R.drawable.lijiang);
    }

    // 将监听器绑定到标签
    public void clickHandle(View v) {
        imgView.setImageResource(R.drawable.lijiang);
    }

    class MyListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            imgView.setImageResource(R.drawable.lijiang);
        }
    }
}
